<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="ZhangPing">





<title>获取当前activity的屏幕截图 | ZhangPing&#39;s Blog</title>



    <link rel="icon" href="/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/js/script.js"></script>
    
    <script src="/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 4.2.0"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/">ZhangPing&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/">ZhangPing&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/archives">Posts</a>
                
                    <a class="menu-item" href="/category">Categories</a>
                
                    <a class="menu-item" href="/tag">Tags</a>
                
                    <a class="menu-item" href="/about">About</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">获取当前activity的屏幕截图</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">ZhangPing</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">February 22, 2016&nbsp;&nbsp;20:25:00</a>
                        </span>
                    
                    
                        <span class="post-category">
                    Category:
                            
                                <a href="/categories/Android-%E7%9F%A5%E8%AF%86%E7%82%B9%E6%95%B4%E7%90%86/">Android-知识点整理</a>
                            
                        </span>
                    
                </div>
            
        </header>

        <div class="post-content">
            <p>此方法是通过view的方式获取当前activity的屏幕截图，并不是framebuffer的方式，所以有一定的局限性。但是这种方法相对简单，容易理解。</p>
<p>首先通过下面的函数获取Bitmap格式的屏幕截图</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Bitmap <span class="title">myShot</span><span class="params">(Activity activity)</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 获取windows中最顶层的view</span></span><br><span class="line">  View view = activity.getWindow().getDecorView();</span><br><span class="line">  view.buildDrawingCache();</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 获取状态栏高度</span></span><br><span class="line">  Rect rect = <span class="keyword">new</span> Rect();</span><br><span class="line">  view.getWindowVisibleDisplayFrame(rect);</span><br><span class="line">  <span class="keyword">int</span> statusBarHeights = rect.top;</span><br><span class="line">  Display display = activity.getWindowManager().getDefaultDisplay();</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 获取屏幕宽和高</span></span><br><span class="line">  <span class="keyword">int</span> widths = display.getWidth();</span><br><span class="line">  <span class="keyword">int</span> heights = display.getHeight();</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 允许当前窗口保存缓存信息</span></span><br><span class="line">  view.setDrawingCacheEnabled(<span class="keyword">true</span>);</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 去掉状态栏</span></span><br><span class="line">  Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), <span class="number">0</span>,</span><br><span class="line">          statusBarHeights, widths, heights - statusBarHeights);</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 销毁缓存信息</span></span><br><span class="line">  view.destroyDrawingCache();</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">return</span> bmp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>得到bitmap格式的图片后，可以把它保存到sd卡中，也可以用下面的方式直接显示到ImageView上：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 将bitmap转换成drawable</span></span><br><span class="line">BitmapDrawable bd=<span class="keyword">new</span> BitmapDrawable(myShot());</span><br><span class="line"> </span><br><span class="line">imageView.setBackgroundDrawable(bd);  </span><br><span class="line"> </span><br><span class="line">imageView.setImageBitmap(myShot());</span><br></pre></td></tr></table></figure>

<p>如果想写入sd中保存，可以使用下面的方法：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">saveToSD</span><span class="params">(Bitmap bmp, String dirName,String fileName)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line"><span class="comment">// 判断sd卡是否存在</span></span><br><span class="line"><span class="keyword">if</span> (Environment.getExternalStorageState().equals(</span><br><span class="line">        Environment.MEDIA_MOUNTED)) &#123;</span><br><span class="line">    File dir = <span class="keyword">new</span> File(dirName);</span><br><span class="line">    <span class="comment">// 判断文件夹是否存在，不存在则创建</span></span><br><span class="line">    <span class="keyword">if</span>(!dir.exists())&#123;</span><br><span class="line">        dir.mkdir();</span><br><span class="line">    &#125;</span><br><span class="line">     </span><br><span class="line">    File file = <span class="keyword">new</span> File(dirName + fileName);</span><br><span class="line">    <span class="comment">// 判断文件是否存在，不存在则创建</span></span><br><span class="line">    <span class="keyword">if</span> (!file.exists()) &#123;</span><br><span class="line">        file.createNewFile();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    FileOutputStream fos = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        fos = <span class="keyword">new</span> FileOutputStream(file);</span><br><span class="line">        <span class="keyword">if</span> (fos != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 第一参数是图片格式，第二个是图片质量，第三个是输出流</span></span><br><span class="line">            bmp.compress(Bitmap.CompressFormat.PNG, <span class="number">100</span>, fos);</span><br><span class="line">            <span class="comment">// 用完关闭</span></span><br><span class="line">            fos.flush();</span><br><span class="line">            fos.close();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (FileNotFoundException e) &#123;</span><br><span class="line">        <span class="comment">// TODO Auto-generated catch block</span></span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">        <span class="comment">// TODO Auto-generated catch block</span></span><br><span class="line">        e.printStackTrace();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>dirName是输出的文件夹名称，filaName是输出的文件名，两者共同组成输出的路径，如“ /mnt/sdcard/pictures/shot.png”。还要注意在AndroidManifest中注册写入权限：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;uses-permission android:name=<span class="string">"android.permission.WRITE_EXTERNAL_STORAGE"</span> /&gt;</span><br></pre></td></tr></table></figure>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>ZhangPing</span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>Permalink:</span>
                        <span><a href="https://zhangpingv5.gitee.io/2016/02/22/%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8Dactivity%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE/">https://zhangpingv5.gitee.io/2016/02/22/%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8Dactivity%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE/</a></span>
                    </p>
                
                
                    <p class="copyright-item">
                        <span>License:</span>
                        <span>转载请注明出处</span>
                    </p>
                
                
                     <p class="copyright-item">
                         <span>Slogan:</span>
                         <span>Self-descipline grants me freedom.</span>
                     </p>
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                    
                        <a href="/tags/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE/"># 屏幕截图</a>
                    
                        
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/2016/04/09/360%E5%8A%A0%E5%9B%BA%E5%8A%A9%E6%89%8B%E5%8A%A0%E5%9B%BA%E6%B5%81%E7%A8%8B/">360加固助手加固流程</a>
            
            
            <a class="next" rel="next" href="/2016/01/15/Android%20Studio%20%E5%BF%AB%E6%8D%B7%E9%94%AE%20for%20Windows/">Android Studio 快捷键 for Windows</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© ZhangPing | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
